<?php
function getFilesByPath($src_path) {
    $fileArray = array();
    $tmpArray = array();
    if ( $dh = opendir ( $src_path ) ) {
        while ( false !== ( $dat = readdir ( $dh ) ) ) {
            if ( $dat != "." && $dat != ".." ) {
                 array_push($tmpArray,"$src_path/$dat");
            }
        }
        closedir($dh);
        sort($tmpArray);
     }
    
     foreach($tmpArray as $dst_file) {
         $info = getimagesize($dst_file);
         if ($info) {
             if ($info[2] == IMAGETYPE_GIF) {
                 array_push($fileArray,array($info[0] , $info[1] , $dst_file));
             } else if ($info[2] == IMAGETYPE_JPEG) {
                 array_push($fileArray,array($info[0] , $info[1] , $dst_file));
             }  else if ($info[2] == IMAGETYPE_PNG) {
                 array_push($fileArray,array($info[0] , $info[1] , $dst_file));
             }
         }
     }
     return $fileArray;
}

function getCarrierNameById($carrier_id) {
    if ($carrier_id==1){
        return "docomo";
    }
    else if ($carrier_id ==2){
        return  "au";
    }
    else if ($carrier_id == 3){
         return  "softbank";
    }
    return "";
}

function ImagickResizeImage($srcFile,$destFile,$rate)
{
        $src = new Imagick($srcFile);
        $dest = new Imagick();
        $color_transparent = new ImagickPixel("transparent");
        foreach($src as $img){
                $page = $img->getImagePage();
                $new_w=round($page['width']*$rate);
                $new_h=round($page['height']*$rate);
                $tmp = new Imagick();
                $tmp->newImage($page['width'], $page['height'], $color_transparent, 'gif');
                $tmp->compositeImage($img, Imagick::COMPOSITE_OVER, $page['x'], $page['y']);
                $tmp->thumbnailImage($new_w, $new_h, true);
                $dest->addImage($tmp);
                $dest->setImagePage($tmp->getImageWidth(), $tmp->getImageHeight(), 0, 0);
                $dest->setImageDelay($img->getImageDelay());
                $dest->setImageDispose($img->getImageDispose());

        }
        $dest->coalesceImages();
        $dest->writeImages($destFile, true);

        $dest->clear();
}

function makeScreenSaver($cid,$carrier_id) {
    $carrier_name= getCarrierNameById($carrier_id);
    $src_path="/var/www/vhdocs/mobile/video/".$cid."/". $carrier_name."/images/thumbnail";
    $dst_path="/var/www/vhdocs/mobile/video/".$cid."/". $carrier_name."/images/screensaver";

    try {
        $datas =  getFilesByPath($src_path);
        $first = new Imagick();
        list($width,$height,$temp)= current($datas);
        $first->newImage($width*3, $height*5, new ImagickPixel('white'));
        $i = 0;
        foreach ($datas as $data) {
            list($x, $y, $file) = $data;
            $xpos = round(abs($i) % 3) * $x ;
            $ypos = round(abs($i+2) / 3 - 1) * $y ;
            $i++;

            $dst_img =  new Imagick($file);
            if ($dst_img) {
                $first->compositeImage($dst_img, Imagick::COMPOSITE_DEFAULT, $xpos, $ypos);
                $dst_img->destroy();
            }
        }
        $first->thumbnailImage(240,400,0);
        $first->writeImages("$dst_path/$cid".".gif", true);
        $first->destroy();
    } catch(Exception $e) {
        echo "makeScreenSaver Error:", $e->getMessage(),"\n";
    }

}

function makeAnimation($cid,$carrier_id) {
    $carrier_name= getCarrierNameById($carrier_id);
    $src_path="/var/www/vhdocs/mobile/video/".$cid."/". $carrier_name."/images/thumbnail";
    $dst_path="/var/www/vhdocs/mobile/video/".$cid."/". $carrier_name."/images/deco";

    try {
        $datas =  getFilesByPath($src_path);

        $im = new Imagick();

        $i = 0;
        foreach ($datas as $data) {
            list($x, $y, $file) = $data;
            if($i == 0) {
                 $frame_img = new Imagick($file);
                 $im->addImage($frame_img);
                 $im->setImageIterations(0);
                 $im->setImageDelay(20);
                 $frame_img->destroy();
                 
            }else{

                $frame_img =  new Imagick($file);
                if ($frame_img) {
                    $im->addImage($frame_img);
                   // if($i==0) {
                   //     $im->setImageIterations(0) ;
                   // }
                    $im->setImageDelay(20);
                    $frame_img->destroy();
                }
            }
            if ($i > 8) {
                break;
            }
            $i++;
        }

        $im->setImageFormat('gif');
        //header('Content-type: image/gif');
        $im->writeImages("$dst_path/$cid"."_00.gif", true);

        $im->destroy();
        $size = filesize("$dst_path/$cid"."_00.gif")/1024;
        if($size > 25) {
            $rate=sqrt(25/$size);
            ImagickResizeImage("$dst_path/$cid"."_00.gif","$dst_path/$cid"."_00_new.gif",$rate);
            unlink("$dst_path/$cid"."_00.gif");
            rename("$dst_path/$cid"."_00_new.gif","$dst_path/$cid"."_00.gif");
        }
    } catch (Exception $e) {
         echo "makeAnimation Error:",$e->getMessage(),"\n";
    }
}

function imagecreatefrom_file($file)
{
	$mrg = NULL;
	$info = getimagesize($file);
	if ($info) {
		if ($info[2] == IMAGETYPE_GIF)
			$mrg = imagecreatefromgif($file);
		else if ($info[2] == IMAGETYPE_JPEG)
			$mrg = imagecreatefromjpeg($file);
		else if ($info[2] == IMAGETYPE_PNG)
			$mrg = imagecreatefrompng($file);
	}
	return $mrg;
}

function imagecopymerge_array($src, $datas)
{
	foreach ($datas as $data) {
		list($x, $y, $file) = $data;

		$mrg = imagecreatefrom_file($file);
		
		if ($mrg) {
			$mrg_width = imagesx($mrg);
			$mrg_height = imagesy($mrg);
			imagecopymerge($src, $mrg, $x, $y,
			0, 0, $mrg_width, $mrg_height, 100);
			imagedestroy($mrg);
		} 

	} 
}


?>

